--[[
/*******************************************************************************

  Este programa está basado en el programa Sokoban desarrollado por el LIFIA - 
  Facultad de Informatica - Univ. Nacional de La Plata

  Copyright 2011 PLADEMA - Facultad de Ciencias Exactas - UNICEN
  Copyright 2010 LIFIA - Facultad de Informatica - Univ. Nacional de La Plata

********************************************************************************

    This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation, either version 2 of the License.

    Ginga is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with
  this program. If not, see <http://www.gnu.org/licenses/>.

********************************************************************************

    Este programa es Software Libre: Ud. puede redistribuirlo y/o modificarlo
  bajo los términos de la Licencia Pública General GNU como es publicada por la
  Free Software Foundation, según la versión 2 de la licencia.

    Ginga se distribuye esperando que resulte de utilidad, pero SIN NINGUNA
  GARANTÍA; ni siquiera la garantía implícita de COMERCIALIZACIÓN o ADECUACIÓN
  PARA ALGÚN PROPÓSITO PARTICULAR. Para más detalles, revise la Licencia Pública
  General GNU.

    Ud. debería haber recibido una copia de la Licencia Pública General GNU
  junto a este programa. Si no, puede verla en <http://www.gnu.org/licenses/>.

*******************************************************************************/
]]--

-- Este módulo representará el "Model" de un juego de Tateti.
module ("TatetiGame", package.seeall);

TatetiGame = {
		-- Constantes
		FREE = 0,
		CROSS = 1,
		CIRCLE = 2,

		-- Jugador actual
		actualPlayer = 1,
		actualI = 1,
		actualJ = 1,

		-- Tablero en el que se mantendrá el estado del juego.
		-- 0: Libre. 1: Player 1. 2: Player 2.
		board = {}
		
}

Class_Metatable = { __index = TatetiGame }


function TatetiGame:isWinner(player)
	if (self.board[0][0] == player and self.board[0][1] == player and self.board[0][2] == player) or
	   (self.board[1][0] == player and self.board[1][1] == player and self.board[1][2] == player) or
	   (self.board[2][0] == player and self.board[2][1] == player and self.board[2][2] == player) or
	   (self.board[0][0] == player and self.board[1][0] == player and self.board[2][0] == player) or
	   (self.board[0][1] == player and self.board[1][1] == player and self.board[2][1] == player) or
	   (self.board[0][2] == player and self.board[1][2] == player and self.board[2][2] == player) or
	   (self.board[0][0] == player and self.board[1][1] == player and self.board[2][2] == player) or
	   (self.board[0][2] == player and self.board[1][1] == player and self.board[2][0] == player) then
		return true
	else
		return false
	end
end


function TatetiGame:changePlayer ()
	if self.actualPlayer == self.CROSS then
		self.actualPlayer = self.CIRCLE;
	else
		self.actualPlayer = self.CROSS;
	end 
end

function TatetiGame:resetBoard ()
	for i = 0, 2 do
		self.board[i] = {};
      		for j = 0, 2 do
        		self.board[i][j] = self.FREE;
      		end
    	end
end

function TatetiGame:new ()
	
	self:resetBoard()	

	return setmetatable ( {}, Class_Metatable )
end

function TatetiGame:reset ()
	self:resetBoard()
	self.actualPlayer = 1
	self.actualI = 1
	self.actualJ = 1
end

-- Es empate...
function TatetiGame:isTie ()
	for i = 0, 2 do
		for j = 0, 2 do
        		if self.board[i][j] == self.FREE then
				return false
			end
      		end
    	end

	return true
end

function TatetiGame:move (i, j)
	if i == 0 or j == 0 then
		self.actualI = math.fmod(self.actualI + 3 + i, 3)
		self.actualJ = math.fmod(self.actualJ + 3 + j, 3)
	end
end

function TatetiGame:playActualPlayer ()
	-- Si no esta ocupado...
	if not self:occuped(self.actualI, self.actualJ) then
		self.board[self.actualI][self.actualJ] = self.actualPlayer;
	end
end
	
function TatetiGame:getBoardValue (i, j)
	return self.board[i][j];
end
	
function TatetiGame:occuped (i, j)
	return self.board[i][j] ~= self.FREE;
end

function TatetiGame:getActualPos ()
	return self.actualI, self.actualJ
end

function TatetiGame:getActualPlayer()
	return self.actualPlayer
end


